import 'package:better_player/better_player.dart';
import 'package:changxindianzishangwu/util/colors.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_message.dart';
import 'package:video_player/video_player.dart';

class VideoMessage extends StatefulWidget {
  VideoMessage({this.message, this.url, this.snapshotUrl, this.type = 1});
  final String url;
  final String snapshotUrl;
  final V2TimMessage message;
  final int type;
  @override
  _VideoMessageState createState() => _VideoMessageState();
}

class _VideoMessageState extends State<VideoMessage> {
  VideoPlayerController videoPlayerController;

  BetterPlayerController controller;

  String videoUrl;
  String snpUrl;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (widget.type == 1) {
      videoUrl = widget.message.videoElem.videoUrl;
      snpUrl = widget.message.videoElem.snapshotUrl;
    } else {
      videoUrl = widget.url;
      snpUrl = widget.snapshotUrl;
    }
    videoPlayerController = VideoPlayerController.network(videoUrl)
      ..initialize().then((value) {
        setState(() {});
      });
    setState(() {});
  }

  @override
  void dispose() {
    /**
     * 页面销毁时，视频播放器也销毁
     */
    videoPlayerController.dispose();
    super.dispose();
    controller.dispose();
  }

//播放器
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Container(
          height: double.infinity,
          padding:
              EdgeInsets.only(top: MediaQuery.of(context).padding.top + 20),
          color: Colors.black,
          alignment: Alignment.center,
          child: AspectRatio(
            aspectRatio: videoPlayerController.value.aspectRatio,
            child: BetterPlayer.network(
              videoUrl,
              betterPlayerConfiguration: BetterPlayerConfiguration(
                autoPlay: false,
                autoDispose: true,
                aspectRatio: videoPlayerController.value.aspectRatio,
                fullScreenAspectRatio: videoPlayerController.value.aspectRatio,
                placeholder: Image.network(snpUrl),
                controlsConfiguration: BetterPlayerControlsConfiguration(
                    playerTheme: BetterPlayerTheme.material,
                    enableMute: false,
                    enableFullscreen: false,
                    enableSkips: false,
                    enableOverflowMenu: false),
                errorBuilder: (context, a) {
                  return Text("视频不见了");
                },
              ),
            ),
          ),
        ),
        Positioned(
            top: MediaQuery.of(context).padding.top,
            child: IconButton(
              icon: Image.asset(
                "images/message/back.png",
                height: width(40),
                width: width(40),
              ),
              onPressed: () => Get.back(),
            ))
      ],
    );
  }
}
